from collections import OrderedDict

class Selector:
    
    def __init__(self,sid,name):
        self.sid = sid
        self.name = name
        self.values = OrderedDict()
        self.subdirs = OrderedDict()
        
    def put(self,value,desc,subdir=None):
        self.values[value] = desc
        self.subdirs[value] = subdir
        
    def get(self,value):
        return self.values[value]
    
    def getSubdir(self,value):
        return self.subdirs[value]

class Special:
    
    def __init__(self,name,desc,getlistsql,getsql):
        self.name = name
        self.desc = desc
        self.getlistsql = getlistsql
        self.getsql = getsql

class FieldType :
    Integer = 1
    Long = 2
    String = 3
    List = 4
    Select = 5
    Customer = 6
    SimpleList = 7
    Float = 8
    Special = 9
    SelectList = 10
    Resource = 11

class Field:

    def __init__(self,name,ftype,desc,param = None,lang = False,nullable = False,depend = None,maxlen = 500):
        self.name = name
        self.ftype = ftype
        self.desc = desc
        self.param = param
        self.lang = lang
        self.nullable = nullable
        self.depend = depend
        self.maxlen = maxlen

class Customer:
    
    def __init__(self,name,path):
        self.name = name
        self.path = path
        self.fields = OrderedDict()
        
    def putField(self,field):
        self.fields[field.name] = field
        
    def getField(self,fieldName):
        return self.fields[fieldName]

class Menu:
    
    def __init__(self,mid,name):
        self.mid = mid
        self.name = name
        self.elements = OrderedDict()

    def putElement(self,element):
        self.elements[element.key] = element

    def getElement(self,key):
        return self.elements[key]

class DicElement:
    
    def __init__(self,name,path,key,seq,upload=None,deploy=True):
        self.name = name
        self.path = path
        self.key = key
        self.seq = seq
        self.upload = upload
        self.deploy = deploy
        self.fields = OrderedDict()
        
    def putField(self,field):
        self.fields[field.name] = field
        
    def getField(self,fieldName):
        return self.fields[fieldName]
    
    def getFieldCount(self):
        return len(self.fields)
    
    def getCaption(self):
        return self.key.capitalize()

class ComponentMgr:
    
    def __init__(self):
        self.selectors = OrderedDict()
        self.customers = OrderedDict()
        self.rawtypes = OrderedDict()
        self.menus = OrderedDict()
        self.specials = OrderedDict()
        self.rawtypes['int'] = FieldType.Integer
        self.rawtypes['long'] = FieldType.Long
        self.rawtypes['string'] = FieldType.String
        self.rawtypes['list'] = FieldType.List
        self.rawtypes['select'] = FieldType.Select
        self.rawtypes['customer'] = FieldType.Customer
        self.rawtypes['slist'] = FieldType.SimpleList
        self.rawtypes['float'] = FieldType.Float
        self.rawtypes['special']= FieldType.Special
        self.rawtypes['sellist'] = FieldType.SelectList
        self.rawtypes['resource'] = FieldType.Resource

    def putSelector(self,selector):
        self.selectors[selector.sid] = selector
        
    def putCustomer(self,customer):
        self.customers[customer.name] = customer
        
    def putMenu(self,menu):
        self.menus[menu.mid] = menu
        
    def putSpecial(self,special):
        self.specials[special.name] = special
        
    def getSelector(self,selectid):
        return self.selectors[selectid]
    
    def getCustomer(self,customername):
        return self.customers[customername]    
    
    def getRawFieldType(self,fieldstr):
        return self.rawtypes[fieldstr]
    
    def getMenu(self,menuid):
        return self.menus[menuid]
    
    def getSpecial(self,specialname):
        return self.specials[specialname]
    
    def getDic(self,key):
        for menu in self.menus.values():
            dics = menu.elements
            if dics.has_key(key):
                return dics[key]
        return None